#include "cwa.h"
#include<fstream>

// Global variables
extern char LASTFILE[30];
extern int NUMRESLIST;
extern int RESNUM;
extern char LASTRESULT[30];

char userinput[30];

// Global functions used in module
extern char* strprompt(char*);
extern char pause(char*);

// Other functions
void checkword();
void dispabout();
void confirmexit();
void displaylast();
void getcommand(char);
void help();

// For menus
char *mainmenu[] = {"Operate", "Search", "Quit"};
char *searchmenu[] = {"Search", "Filter", "Check word"};
int mmni = 3, gmni = 5, smni = 3, hmni = 2;
char c[6][2];

// Main program
using namespace std;

// Objects

Menu MainMenu(mainmenu, mmni, "Main Menu"); // Menu Objects
Menu SearchMenu(searchmenu, smni, "Search Menu");

extern searchclass Search;
extern ResList reslist;
Grid grid(10);

void checkword() {
	strcpy(userinput, strprompt("WORD TO CHECK: "));
	if (Search.wsearch(userinput, 0))
		pause(strcat(::userinput, " found in dictionary."));
	else
		pause(strcat(::userinput, " - word not found."));
}

void confirmexit() {
	if (toupper(pause("Quit CWA <Y/N>? ")) == 'Y') {
		clrscr();
		flushall();
		exit(0);
	}
}

void displaylast() {
	if (streql(LASTFILE, "NULL"))
		pause("No results to display!");
	else {
		reslist.getitems(RESNUM, LASTFILE);
		strcpy(LASTRESULT, reslist.operate());
	}
}

void helpsrch() {
	Box helper;
	helper.setsize(MENUPOSX, 1, 25, 21);
	helper.draw();
	helper.setitem("INSTRUCTIONS-          ");	helper.boxpos++;
	helper.setitem("Enter the search string");	helper.boxpos++;
	helper.setitem("and append a special   ");	helper.boxpos++;
	helper.setitem("character to choose an ");	helper.boxpos++;
	helper.setitem("operation.");				helper.boxpos++;
	helper.setitem("                       ");	helper.boxpos++;
	helper.setitem("CHAR        OPERATION  ");	helper.boxpos++;
	helper.setitem("                       ");	helper.boxpos++;
	helper.setitem(" *          Anagrammer ");	helper.boxpos++;
	helper.setitem(" %          LetterSet  ");	helper.boxpos++;
	helper.setitem(" #          Substring  ");	helper.boxpos++;
	helper.setitem(" *          Any Of Set ");	helper.boxpos++;
	helper.setitem("                       ");	helper.boxpos++;
	helper.setitem(" Add ! for complement.");	helper.boxpos++;
	helper.setitem(" Add | for filter      ");	helper.boxpos++;
	helper.setitem("                       ");	helper.boxpos++;
	helper.setitem("Press ENTER after      ");	helper.boxpos++;
	helper.setitem("typing command.        ");	helper.boxpos++;
	helper.deactivate();
}

void getcommand(char c) {

	helpsrch();
	strcpy(::userinput, strprompt("ENTER COMMAND: "));
	if (c == 'F')
		strcat(userinput, "|");
	Box helper;
	helper.setsize(MENUPOSX, 1, 25, 21);
	helper.undraw();
	window(1, 1, 80, 25);

	if (Search.wsearch(userinput, 0))
		displaylast();
	else
		pause("Found no matches.");

}

// Driver program

void main() {
	textbackground(BGColor);
	textcolor(FGColor);
	clrscr();
	grid.drawgrid();
	grid.fillgrid();
	grid.operate();

	do {
		strncpy(c[0], MainMenu.operate(), 2);
		switch (c[0][0]) {
		case 'S':
			strncpy(c[2], SearchMenu.operate(), 2);
			switch (c[2][0]) {
			case 'F':
			case 'S':
				getcommand(c[2][0]);
				break;
			case 'C':
				checkword();
				break;
			}
			break;

		case 'O':
			grid.operate();
			break;
		case 'R':
			displaylast();
			break;

		case ' ':
			c[0][0] = 'Q';
		case 'Q':
			confirmexit();
			c[0][0] = ' ';
			break;
		}
	}
	while (c[0][0] != 'Q');
}
